import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/LoginView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/login/RegisterView.vue')
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import('../views/dashboard/DashboardView.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'hotels',
          name: 'hotels',
          component: () => import('../views/hotels/HotelsView.vue')
        },
        {
          path: 'rooms/:hotelId',
          name: 'rooms',
          component: () => import('../views/rooms/RoomsView.vue')
        },
        {
          path: 'orders',
          name: 'orders',
          component: () => import('../views/orders/OrdersView.vue')
        }
      ]
    }
  ]
})

// 全局前置守卫，用于检查用户是否已登录
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      next('/login')
      return
    }
  }
  next()
})

export default router 